<?php
/**
 * 公告列表管理
 * @author a
 *
 */
class Affiche extends CT_Controller{
    
    private $offse = 0;
    private $page_size = 0;
    private $page = 0;
    private $size = 10; //每页显示数量
    private $substr_num = 20;   //列表显示留言数量
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->load->helper("tools_helper");   //加载工具
        $this->t = $this->ct_template;
        
        //$this->_nursery_id = 1;     //重置园所ID为1
        
        $this->load->model('affiche_model');
        
        //分页
        $this->page = $this->input->get('per_page',TRUE) ? intval($this->input->get('per_page',TRUE)) : 1;
        $this->page_size = $this->config->item('per_page')?$this->config->item('per_page'):$this->size;
        $this->offse = ($this->page-1) * $this->page_size;
        
    }
    
    /**
     * 分类列表
     */
    public function Index(){
        try {
        	
            $affiche_list = $this->affiche_model->findAll(array('status'=>1),'*','weight desc',$this->offse,$this->page_size);
            if(empty($affiche_list))throw new Exception('没有记录');
            
            $total = $this->affiche_model->findCount(array('status'=>1)); //总数
            $pagelink = $this->pagination($total,$this->page_size);
            
            
            //echo '<pre>';print_r($affiche_list);die();
            
            
        }catch (Exception $e){
            $error = $e->getMessage();
        }
        $this->t->write_view('content', 'admin/affiche/index', array(
            'pagelink'     =>  $pagelink,
            'affiche_list'    =>  $affiche_list,
    		'substr_num'   =>   $this->substr_num,
            'error'     =>  empty($error) ? '' : $error,
        ), false);
        $this->t->render();
    }
    /**
     * 详情
     * @throws Exception
     */
    public function Info(){
    	try {
    		$id = intval($_REQUEST['id']);
    		if(empty($id))throw new Exception('ID为空');
    	
    		$affiche_info = $this->affiche_model->find(array('id'=>$id));
    		if(empty($affiche_info))throw new Exception('不存在');
    	
    		$this->t->write_view('content', 'admin/affiche/info', array(
    				'affiche_info'  =>  $affiche_info,
    		), false);
    		$this->t->render();
    	}catch (Exception $e){
    		$error = $e->getMessage();
    		$this->t->write_view('content', 'admin/msg_error', array(
    				'error'     =>  $error,
    				'time'  =>  10,
    				'url'   =>  '/admin/affiche/',
    		), false);
    		$this->t->render();
    	}
    }
    /**
     * 修改排序
     */
    public function sort(){
        try {
            $return = array('code'=>10,'msg'=>'修改成功');
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('ID为空');
            $sort = intval($this->input->get('sort'));
            
            $bool = $this->affiche_model->update(array('weight'=>$sort),array('id'=>$id));
            
            if(empty($bool))throw new Exception('修改失败');
            
        }catch (Exception $e){
            $return['code'] = 11;
            $return['msg'] = $e->getMessage();
        }
        echo json_encode($return);
    }
    /**
     * 修改
     */ 
    public function Update(){
        try {
            $id = intval($_REQUEST['id']);
            if(empty($id))throw new Exception('ID为空');
            $form_submit = $this->input->post('form_submit');
            if(!empty($form_submit)){
                $data = array();
                $data['title'] = $this->input->post('title');
                $data['content'] = $this->input->post('content');
                $data['update_date'] = date('Y-m-d H:i:s');
                $bool = $this->affiche_model->update($data,array('id'=>$id));
                if(empty($bool))throw new Exception('修改失败');
                redirect("/admin/affiche");
            }
            
            $affiche_info = $this->affiche_model->find(array('id'=>$id));
            
            $this->t->write_view('content', 'admin/affiche/update', array(
                'affiche_info'  =>  $affiche_info,
            ), false);
            $this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/affiche/',
            ), false);
            $this->t->render();  
        }
    }
    /**
     * 添加
     */
    public function Add(){
        try {
            $form_submit = $this->input->post('form_submit');
            if(!empty($form_submit)){
                $data = array();
                $data['title'] = $this->input->post('title');
                $data['content'] = $this->input->post('content');
                $data['create_date'] = date('Y-m-d H:i:s');
                $bool = $this->affiche_model->insert($data);
                if(empty($bool))throw new Exception('添加失败');
                redirect("/admin/affiche");
            }
            
            $this->t->write_view('content', 'admin/affiche/add', array(
            		
            ), false);
            $this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/affiche/',
            ), false);
            $this->t->render(); 
        }
    }

    /**
     * 删除
     */
    public function Delete(){
        try {
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('ID为空');
        
            $bool = $this->affiche_model->update(array('status'=>0),array('id'=>$id));
            if(empty($bool))throw new Exception('删除失败');
            redirect("/admin/affiche");
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/affiche/',
            ), false);
            $this->t->render();            
        }
    }
}






























